<%@ page language="java" pageEncoding="UTF-8" contentType="text/html;charset=UTF-8"%>
<%@ include file="checkConfig.jsp"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Welcome to Pathlet E-Shop</title>
<jsp:include page="header.jsp"><jsp:param name="groups" value="jquery,jquery-ui,validation,common"/></jsp:include>
<script type="text/javascript">
$(document).ready(function() {
	
	var loginURL = 'party/AuthenticationAction/login.do';
	var getCurrentConfigURL = 'config/DataSourceAction/getCurrentConfig.do';
	var mainURL = 'main.jsp';

	var validator=$('#loginForm').validate({
		 rules:{
              username:{required:true, minlength:3},
              password:{required:true, minlength:3}
         }
    }); 

	//Load and show the current DataSourceConfig.
	function checkInitialization() {
		$.ajax({
			async: false,
			url: getCurrentConfigURL,
			success: function(result){
				if(result.dataInitialized == false) {
					$.msgBox('yesno', 'Pathlet Initialization', 
							'Data source has not been initialized! Do you want intialized it now?', function(result) {
						if(result == true) {
							window.location.href='config/dataSource.jsp?initialize=true';
						}
					});
				}
				else {
					$('#loginForm').show();
				}
			},
			complete: function(result){
				$('#loginForm').show();
			}
		});
	}
	
	checkInitialization();
	
	
	$('#submitForm').click(function() {
		doLogin();
		return false;
	});
	
	$('#resetForm').click(function() {
		$('#loginForm').resetForm();
		return false;
	});
	
	//Listening the return key down event
	$(document).keydown(function(event){ 
		if(event.keyCode == 13) { 
			doLogin();
		}
	});

	$('#loginForm input[name=username]').focus();
	

	function doLogin() {
		if(!validator.form()){ 
	         validator.focusInvalid();
	         return false;
	    }
		
		var postData = $('#loginForm').serialize();
		$.ajax({
			type: 'POST',
			dataType: 'json',
			url: loginURL,
			data: postData,
			success: function(result){
				if(result != null && result.level == 'INFO') {
					window.location.href = mainURL;
				}
				else {
					$('#password').val('');
					$.msgBox('error', 'Login failed', result.message);
				}
			}
		});
	}
	


});
</script>
</head>
<body>
<form method="post" id="loginForm" style="display:none;">
  <table width="400" height="80" border="0" align="center" cellpadding="0" cellspacing="0">
    <tr>
      <td>&nbsp;</td>
    </tr>
  </table>
  <div style="text-align:center;">
  <h1>Welcome to Pathlet E-Shop Example</h1>
  </div>
  <table width="400" align="center" class="ui-widget ui-widget-content">
  	<thead>
    <tr class="ui-widget-header">
      <td colspan="2"><strong>Sign in</strong></td>
    </tr>
    </thead>
    <tr>
      <td width="85" align="right">Username: </td>
      <td width="285"><input id="username" name="username"/></td>
    </tr>
    <tr>
      <td align="right">Password: </td>
      <td width="100"><input type="password" name="password" id="password"/></td>
    </tr>
    <tr>
      <td colspan="2" align="center">Default username/password is <span style="color:red;">admin/admin</span></td>
    </tr>
    <tr>
      <td colspan="2" align="center"><button id="submitForm">Sign in</button>&nbsp;
        <button id="resetForm">reset</button>
       
       </td>
    </tr>
  </table>

</form>

</body>
</html>
